/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureTypeWeaponDAO;
import cz.muni.fi.pa165.creatures.dao.utils.DAOUtils;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import cz.muni.fi.pa165.creatures.model.CreatureTypeWeapon;
import cz.muni.fi.pa165.creatures.model.Weapon;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.CreatureTypeWeapon} instances.
 *
 * @author smikloso
 */
@Repository
@Transactional
public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void create(CreatureTypeWeapon creatureTypeWeapon) {
        DAOUtils.notNull(creatureTypeWeapon);
        em.persist(creatureTypeWeapon);
    }

    @Override
    public void remove(CreatureTypeWeapon creatureTypeWeapon) {
        DAOUtils.notNull(creatureTypeWeapon);
        CreatureTypeWeapon toRemove = get(creatureTypeWeapon.getCreatureType(), creatureTypeWeapon.getWeapon());
        em.remove(toRemove);
    }

    @Override
    public void update(CreatureTypeWeapon creatureTypeWeapon) {
        DAOUtils.notNull(creatureTypeWeapon);
        DAOUtils.notNull(creatureTypeWeapon.getCreatureType());
        DAOUtils.notNull(creatureTypeWeapon.getWeapon());
        DAOUtils.notNull(creatureTypeWeapon.getEffectivity());
        em.merge(creatureTypeWeapon);
    }

    @Override
    public CreatureTypeWeapon get(CreatureType creatureType, Weapon weapon) {
        DAOUtils.notNull(creatureType);
        DAOUtils.notNull(weapon);

        TypedQuery<CreatureTypeWeapon> query =
                em.createQuery(
                    "select ctw from CreatureTypeWeapon ctw where "
                    + "creatureType = :creatureType "
                    + "and "
                    + "weapon = :weapon",
                    CreatureTypeWeapon.class);

        query.setParameter("creatureType", creatureType);
        query.setParameter("weapon", weapon);
        try {
            return query.getSingleResult();
        } catch (NoResultException ex) {
            // if no record is returned, this exception is thrown,
            // which indicates we return null
            return null;
        }
    }

    @Override
    public Long getCount() {
        Query q = em.createQuery("select count(o) "
                + "from CreatureTypeWeapon as o");
        return ((Long) q.getSingleResult());
    }

    @Override
    public List<CreatureTypeWeapon> getAll() {
        Query q = em.createQuery("select cwt from CreatureTypeWeapon cwt "
                + "order by cwt.creatureType, cwt.weapon");
        return q.getResultList();
    }
}